package ilincar.systemcore;


import ilincar.systemcore.service.CoreService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

public class MainActivity extends Activity {

    private final String TAG = "MainActivity";
    private CoreService mCoreService = null;
    private boolean mIsBound = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        doBindService();
        
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void doBindService(){
        Intent it = new Intent(MainActivity.this, CoreService.class);
        startService(it);
        bindService(it, mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
        it = null;
    }

    private void doUnbindService(){
        if(mIsBound) {
            unbindService(mConnection);
            mIsBound = false;
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mCoreService = ((CoreService.ServiceBinder)iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mCoreService = null;
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }
}
